package com.cn.ZhiKeDianShang.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cn.ZhiKeDianShang.AppConfig;
import com.cn.ZhiKeDianShang.R;
import com.cn.ZhiKeDianShang.base.BaseActivity;
import com.cn.ZhiKeDianShang.model.response.UserInfoBean;
import com.cn.ZhiKeDianShang.presenter.LoginUserPresenter;
import com.cn.ZhiKeDianShang.presenter.view.LoginUserView;
import com.cn.ZhiKeDianShang.util.ToastUtil;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.HttpUtils;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.ObjectResult;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.Result;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/7/3.
 * do what to 2017/7/3
 */

public class UpdatePwdActivity extends BaseActivity<LoginUserView, LoginUserPresenter> implements LoginUserView {

    @BindView(R.id.et_oldpwd)
    EditText etOldpwd;
    @BindView(R.id.et_newpwd)
    EditText etNewpwd;
    @BindView(R.id.et_newpew1)
    EditText etNewpew1;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    @Override
    protected int getNormalLayoutId() {
        return R.layout.activity_update_pwd;
    }

    @Override
    protected int getErrorLayoutId() {
        return R.layout.activity_update_pwd;
    }

    @Override
    protected LoginUserPresenter createPresenter() {
        return new LoginUserPresenter(this, this);
    }

    @Override
    protected void initViewsAndEvents() {
    }


    /**
     * @param
     */
    private void updatePwd() {
//        DialogHelper
        Map<String, String> params = new HashMap<>();
        params.put("oldpassword", etOldpwd.getText().toString());
        params.put("password", etNewpwd.getText().toString());
        params.put("repassword", etNewpew1.getText().toString());

        HttpUtils.post().url(AppConfig.USER_CHANGEPWD)
                .params(params)
                .addSecret()
                .build()
                .execute(new BaseCallback<LoginRegisterResult>(LoginRegisterResult.class) {
                    @Override
                    public void onResponse(ObjectResult<LoginRegisterResult> result) {
//                        DialogHelper.dismissProgressDialog();
                        Log.d("Debug", "到达请求这里---");
                        if (Result.checkSuccess(UpdatePwdActivity.this, result)) {
                            ToastUtil.showShort("密码修改成功");
                            finish();
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
//                        DialogHelper.dismissProgressDialog();
//                        ToastUtil.showErrorNet(LoginActivity.this);
                    }
                });
    }


    @Override
    public void onLogin(UserInfoBean resultBean) {

    }

    @Override
    public void requestFailed(String message) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.setting_back, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.setting_back:
                finish();
                break;
            case R.id.tv_submit:
                if (etOldpwd.getText().toString().length() == 0) {
                    ToastUtil.showShort("请输入原密码");
                    return;
                }
                if (etNewpwd.getText().toString().length() == 0) {
                    ToastUtil.showShort("请输入新密码");
                    return;
                }
                if (etNewpew1.getText().toString().length() == 0) {
                    ToastUtil.showShort("请再次输入新密码");
                    return;
                }
                if (!etNewpwd.getText().toString().equals(etNewpew1.getText().toString())) {
                    ToastUtil.showShort("两次密码输入不一致");
                    return;
                }
                updatePwd();
                break;
        }
    }
}
